package com.xt.website_manager.controller;

import com.xt.website_manager.po.News;
import com.xt.website_manager.po.User;
import com.xt.website_manager.service.NewsService;
import com.xt.website_manager.service.UserService;
import com.xt.website_manager.util.MyPageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * @Author: jiangyu
 * @Date: 2019/9/2 11:32
 * @Description:
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;
    @Resource
    private NewsService newsService;

    /**
     * 登录校验
     */
    @RequestMapping("/doLogin")
    public String doLogin(User user, String remeberme, HttpServletResponse response, HttpSession session) {
        // 记录未进行加密前的密码
        String password = user.getPassword();
        //根据用户名，密码查询出的user
        User u = userService.findUserByUnameAndPwd(user);
        if (u == null) {
            // 登录失败
            String msg = "";
            try {
                msg = URLEncoder.encode("用户名或密码错误！", "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return "redirect:/login?msg=" + msg;
        } else {
            //登录成功
            //将user存入session
            u.setPassword(password);
            session.setAttribute("user",u);
            //cookie处理
            Cookie nameCookie = new Cookie("username",u.getUsername());
            Cookie pwdCookie = new Cookie("password",u.getPassword());
            // 判读cookie状态
            if(remeberme!=null){
                nameCookie.setMaxAge(60*60*24*7);
                pwdCookie.setMaxAge(60*60*24*7);
            }else{
                nameCookie.setMaxAge(0);
                pwdCookie.setMaxAge(0);
            }
            nameCookie.setPath("/");
            pwdCookie.setPath("/");

            response.addCookie(nameCookie);
            response.addCookie(pwdCookie);
            return "redirect:/user/myInfo.html";
        }
    }

    /**
     * 退出登录
     */
    @RequestMapping("/loginOut")
    public String loginOut(HttpSession session){
        session.invalidate();
        return "redirect:/login";
    }



    /**
     * 个人信息查看
     */
    @RequestMapping("/myInfo.html")
    public String myInfo(){
        return "myInfo";
    }

    /**
     * 修改个人信息
     */
    @RequestMapping("/updateMyInfo.html")
    public String updateMyInfo(User user,HttpSession session){
        userService.updateUser(user);
        session.invalidate();
        return "redirect:/login";
    }



    /**
     * 用户分页列表
     */
    @RequestMapping("/findAllUsers.html")
    public String findAllUsers(Model model, MyPageInfo<User> pageInfo) {
        pageInfo = userService.findAllUsers(pageInfo);
        model.addAttribute("pageInfo", pageInfo);
        return "users";
    }

    /**
     * 添加用户页面
     */
    @RequestMapping("/addUserPage.html")
    public String addUserPage() {
        return "addUser";
    }


    /**
     * 判断用户名是否已经存在
     */
    @RequestMapping("/checkUsername.html")
    @ResponseBody
    public String checkUsername(String username) {
        User user = userService.checkUsername(username);
        if (user == null) {
            return "ok";
        } else {
            return "no";
        }
    }


    /**
     * 执行添加用户
     */
    @RequestMapping("/doAdd.html")
    public String doAdd(User user) {
        System.out.println(user);
        userService.addUser(user);
        return "redirect:/user/findAllUsers.html";
    }


    /**
     * 删除用户
     */
    @RequestMapping("/delUser.html")
    @ResponseBody
    public String delUser(Integer id) {
        // 查询该用户是否发表过新闻
        News news = newsService.findNewsByUid(id);
        if (news != null) {
            return "no";
        }
        userService.delUset(id);
        return "ok";
    }

    /**
     * 修改用户页面
     */
    @RequestMapping("/updatePage.html")
    public String updatePage(Model model, MyPageInfo<User> pageInfo, Integer id) {
        //查询用户
        User user = userService.userDetails(id);
        //将信息加入model
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("user", user);
        return "updateUser";
    }

    /**
     * 执行修改用户
     */
    @RequestMapping("/doUpdate.html")
    public String doUpdate(Model model, MyPageInfo<User> pageInfo, User user) {
        userService.updateUser(user);
        model.addAttribute("pageInfo", pageInfo);
        return "forward:/user/findAllUsers.html";
    }

}
